package webservices;

import interfaces.IDownloadHistoryQuery;
import interfaces.IFavouriteUpdateQuery;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import webservices.helper.Helper;

public class DownloadHistoryQueryImpl implements IDownloadHistoryQuery {
	
	public String query(String userID){
		String returnMessage = "";
		Connection con = null;
		try {
			con = Helper.getDBConnection();
			returnMessage = "<response name='success' />";
			
			ResultSet rs = con.createStatement().executeQuery("select h.downloadtime as downloadtime, s.name as slidename, c.name as coursename, c.courseid as courseid "
														+ " from downloadhistory h, slide s, course c where h.userid = " + userID 
														+ " and h.slideid = s.slideid and s.courseid = c.courseid order by downloadtime desc");

			while (rs.next()){
				returnMessage += "<response name='downloadHistory'>";
				returnMessage += "<courseID>" + rs.getString("courseid") + "</courseID>" ;
				returnMessage += "<courseName>" + rs.getString("coursename") + "</courseName>" ;
				returnMessage += "<slideName>" + rs.getString("slidename") + "</slideName>" ;
				returnMessage += "<downloadTime>" + rs.getString("downloadtime") + "</downloadTime>" ;
				returnMessage += "</response>";
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try{
				con.close();
			}catch(SQLException e) {}
		}
		return returnMessage;
	}
	
}